package com.codefish;

import java.util.Random;

/**
 * A股票价格预测者
 *
 * @author codefish
 * @version 1.0
 * @date 2022/05/07 下午 04:31
 */
public class PredictObserver extends StockObserver {
    /**
     * 订阅的发布者
     */
    StockPublisher publisher;
    /**
     * 构造函数
     * @param name 订阅者标识
     * @param publisher 订阅的发布者对象
     */
    public PredictObserver(String name, StockPublisher publisher) {
        super.name = name;
        this.publisher = publisher;
    }

    @Override
    public void execute() {
        if(publisher instanceof AStockPublisher){
            double price = ((AStockPublisher) publisher).getPrice();
            Random r = new Random();
            //预测明天股票价格为当前价格的90%-110%
            double preVal = price * (r.nextInt(20)+90)/100;
            System.out.println(name + ":预测明天股票价格为" + preVal);
        }
    }
}
